package at.jku.se.fastsell.db4o;

import java.util.logging.Logger;

import at.jku.se.fastsell.db4o.transactions.Transaction;

/**
 * Handler class for db4o database transactions
 */
public class TransactionManager {

	private static final Logger LOG = Logger.getLogger(TransactionManager.class
			.getName());

	public static <T> T run(Transaction<T> transaction) throws DatabaseException {
			LOG.info("Running transaction '" + transaction.toString() + "'");
			T result = transaction.run();
			// transaction.close();
			LOG.info("Transaction '" + transaction.toString() + "' successful committed.");
			return result;
			// --
	}	// --------------------------------------------------------------------------------------------

	private static <T> T retryTransaction(Transaction<T> transaction)
			throws DatabaseException {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
		}

		LOG.info("Retrying transaction '" + transaction.toString() + "'");
		T result = transaction.run();
		// transaction.close();
		return result;
		// --
	}
}
